package com.xu.blog_after_end.web.admin;

import com.xu.blog_after_end.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class StatisticsController {
    
    @Autowired
    private StatisticsService statisticsService;
    
    @GetMapping("/statistics")
    public String statistics(Model model) {
        // 获取总体统计数据
        model.addAttribute("overall", statisticsService.getOverallStatistics());
        
        // 获取博客发布趋势数据
        model.addAttribute("blogTrend", statisticsService.getBlogPublishTrend());
        
        // 获取分类统计数据
        model.addAttribute("typeStats", statisticsService.getTypeStatistics());
        
        // 获取标签统计数据
        model.addAttribute("tagStats", statisticsService.getTagStatistics());
        
        // 获取访问量最高的博客
        model.addAttribute("topViewedBlogs", statisticsService.getTopViewedBlogs());
        
        // 获取评论最多的博客
        model.addAttribute("mostCommentedBlogs", statisticsService.getMostCommentedBlogs());
        
        return "admin/statistics";
    }
    
    // 提供API接口，用于AJAX请求获取数据
    @GetMapping("/api/statistics")
    @ResponseBody
    public Map<String, Object> getStatisticsData() {
        Map<String, Object> data = new HashMap<>();
        
        data.put("overall", statisticsService.getOverallStatistics());
        data.put("blogTrend", statisticsService.getBlogPublishTrend());
        data.put("typeStats", statisticsService.getTypeStatistics());
        data.put("tagStats", statisticsService.getTagStatistics());
        data.put("topViewedBlogs", statisticsService.getTopViewedBlogs());
        data.put("mostCommentedBlogs", statisticsService.getMostCommentedBlogs());
        
        return data;
    }
}